package com.szq.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.szq.yygh.hosp.model.hosp.Department;
import com.szq.yygh.hosp.model.hosp.Hospital;
import com.szq.yygh.hosp.repository.DepartmentRepository;
import com.szq.yygh.hosp.service.DepartmentService;
import com.szq.yygh.hosp.vo.hosp.DepartmentQueryVo;
import com.szq.yygh.hosp.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * All rights Reserved, Designed By www.jx-lab.com
 * @Title: DepartmentServiceImpl
 * @Description:
 * @author lixy
 * @date 2021/8/19 17:35
 * @version V1.0
 * @Copyright: www.jx-lab.com Inc. All rights reserved.
 * 注意：本内容仅限于众合景轩信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> requestMap) {
        String jsonStr = JSONObject.toJSONString(requestMap);
        Department department = JSONObject.parseObject(jsonStr, Department.class);

        //根据医院编号和科室编号查询是否存在
        Department oldDepartment = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (null != oldDepartment){
            department.setCreateTime(oldDepartment.getCreateTime());
            department.setIsDeleted(0);
        }else{
            department.setCreateTime(new Date());
        }
        department.setUpdateTime(new Date());
        departmentRepository.save(department);
    }

    /**
     * 分页查询科室列表
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Pageable pageable = PageRequest.of(page-1, limit);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> result = departmentRepository.findAll(example, pageable);
        return result;
    }

    /**
     * 删除科室信息
     * @param hoscode
     * @param depcode
     */
    @Override
    public void removeDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (null != department){
            departmentRepository.delete(department);
        }
    }

    /**
     * 根据医院编码查询所有科室信息
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> getDeptList(String hoscode) {
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        List<Department> list = departmentRepository.findAll(example);
        List<DepartmentVo> result = groupByList(list);
        return result;
    }

    /**
     * 获取科室名称
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDeptName(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (null != department){
            return department.getDepname();
        }
        return null;
    }

    private List<DepartmentVo> groupByList(List<Department> departmentList){
        List<DepartmentVo> result = new ArrayList<>();
        Map<String, List<Department>> collect = departmentList.stream()
                            .collect(Collectors.groupingBy(Department::getBigcode));
        for(String key : collect.keySet()){
            List<DepartmentVo> children = new ArrayList<>();
            List<Department> departments = collect.get(key);
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(departments.get(0).getBigname());
            departments.stream().forEach(t->{
                DepartmentVo dv = new DepartmentVo();
                dv.setDepcode(t.getDepcode());
                dv.setDepname(t.getDepname());
                dv.setChildren(new ArrayList<>());
                children.add(dv);
            });
            departmentVo.setChildren(children);
            result.add(departmentVo);
        }
        return result;
    }
}
